package com.ksyun.client.controller;

import com.ksyun.client.domain.dto.DateTimeResponse;
import com.ksyun.client.domain.dto.Response;
import com.ksyun.client.domain.dto.ServiceInstance;
import com.ksyun.client.service.ClientService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class ClientController {
    private final ClientService clientService;
    @GetMapping("/getInfo")
    public ResponseEntity<Response> getInfo(){
        RestTemplate restTemplate = new RestTemplate();
        String url=clientService.geturl();
        if (url!=null){
            try {
                DateTimeResponse response = restTemplate.getForObject(url, DateTimeResponse.class);
                return ResponseEntity.ok(new Response(null,"Hello Kingsoft Cloud Star Camp -"+response.getServiceId()+" - "+response.getResult()));
            }catch (Exception e){
                return ResponseEntity.ok(new Response("true","服务不可用"));
            }
        }else {
            //服务停止
            return ResponseEntity.ok(new Response("true","服务实例不存在"));
        }
    }
    //唤醒操作
    @PostMapping("/awake")
    public ResponseEntity<ServiceInstance> awake(@RequestBody ServiceInstance serviceInstance){
        clientService.awake(serviceInstance);
        return ResponseEntity.ok(serviceInstance);
    }
    //睡眠操作
    @PostMapping("/sleep")
    public ResponseEntity<ServiceInstance> sleep(@RequestBody ServiceInstance serviceInstance){
        clientService.sleep(serviceInstance);
        return ResponseEntity.ok(serviceInstance);
    }
}
